import Taro, { Component } from '@tarojs/taro'
import { View, Text, Block, Image, ScrollView } from '@tarojs/components'

import { Tabbar, Footer, MoreFormid, DataLoading } from './../../../component/index'
import './my.less'
import allRequest from '../../../api/allRequest';

let _this

let enableDebugCounter = 0
class My extends Component {

    config = {
        navigationBarTitleText: '个人中心',
        // enablePullDownRefresh: true,
    }

    constructor() {
        super()
        this.state = {
            avatarUrl: 'https://file.open.banchengyun.com/2019/03/28/155375292386271.png',
            nick_name: '',
            storeName: '',
            list: [],
            myInfo: {},
            userInfo: {},
            company_protocol_title: "",
            changeLoad: true
        }
    }

    componentWillReceiveProps(nextProps) {
        console.log(this.props, nextProps)
    }

    componentDidMount() {
        _this = this
        this.getData()
        //缓存用户处于企业端
        Taro.setStorageSync('situation', 'business')
        //获取微信信息，缓存获取
        let userInfo = Taro.getStorageSync("user")
        userInfo = JSON.parse(userInfo)
        this.setState({ userInfo })

        //获取协议名称
        this.setState({
            company_protocol_title: JSON.parse(Taro.getStorageSync("saasConfig")).company_protocol_title
        })
    }
    // onPullDownRefresh() {
    //     this.getData()
    //     Taro.stopPullDownRefresh()
    // }

    refresh() {
        _this.getData()
    }
    componentWillUnmount() { }

    componentDidShow() { }

    componentDidHide() { }

    getData() {
        allRequest.companyMe()
            .then(res => {
                this.setState({ myInfo: res }, () => {
                    setTimeout(() => { this.setState({ changeLoad: false }) }, 100)
                })
            })
            .catch(err => { console.log(err) })
    }

    enableDebug(e) {
        if (++enableDebugCounter > 10) {
            Taro.setEnableDebug({
                enableDebug: true
            })
        }
    }

    loginRefreshPage() {
        this.getData()
    }

    toWhere(url, type, e) {
        if (type) {
            Taro[type]({
                url
            })
        }
    }

    render() {
        const currentPage = 'businessMy';
        const { avatarUrl, myInfo, userInfo, company_protocol_title, changeLoad } = this.state;
        return (
            <View className='businessMy'>
                {
                    JSON.stringify(myInfo) != '{}' && <ScrollView className='content' scrollY >
                        <View className='header' style={`background:${Taro.getStorageSync('baseColor')}`}>
                            <View className='person-info'>
                                <Image className='avatar' src={userInfo.avatar_url || myInfo.company_principal.company.logo || avatarUrl} onClick={this.enableDebug.bind(this)} />
                                <Block>
                                    <View className='name'>{myInfo.company_principal.company.title || userInfo.nick_name}</View>
                                    <View className='shop-name'>(ID：{myInfo.company_principal.company_id})</View>
                                </Block>
                            </View>
                            <Image className='wave' src='https://file.open.banchengyun.com/2019/03/28/155375296968534.png' />
                            {
                                this.state.isLogined && (<View className='approves-list'>
                                    <View className='item'>
                                        <Image className='icon' src='https://file.open.banchengyun.com/2019/03/28/155375300112767.png' />
                                        <Text className='text' style={`background:${Taro.getStorageSync('baseColor')}`}>企业实体认证</Text>
                                    </View>
                                    <View className='item'>
                                        <Image className='icon' src='https://file.open.banchengyun.com/2019/03/28/155375300112767.png' />
                                        <Text className='text'>企业实名认证</Text>
                                    </View>
                                    <View className='item'>
                                        <Image className='icon' src='https://file.open.banchengyun.com/2019/03/28/155375300112767.png' />
                                        <Text className='text'>营业执照认证</Text>
                                    </View>
                                </View>)
                            }
                        </View>
                        <MoreFormid>
                            <View className='cell'>
                                <View className='row' onClick={this.toWhere.bind(this, '/subPackages/subPackageF/equity/equity', 'navigateTo')}>
                                    <View className='label'>
                                        <Image className='icon' src='https://file.open.banchengyun.com/353/59690a79c667ba052bf125e0413d2a65' />
                                        <View className='name'>我的权益</View>
                                    </View>
                                    <View className='right-icon'></View>
                                </View>
                                <View className='row' onClick={this.toWhere.bind(this, '/subPackages/subPackageF/myShop/myShop', 'navigateTo')}>
                                    <View className='label'>
                                        <Image className='icon' src='https://file.open.banchengyun.com/2019/03/28/155375303924787.png' />
                                        <View className='name'>我的商铺</View>
                                    </View>
                                    <View className='right-icon'></View>
                                </View>
                                <View className='row' onClick={this.toWhere.bind(this, `/subPackages/subPackageF/jobManage/jobManage?auth_status=${myInfo.company_auth ? myInfo.company_auth.auth_status : 0}`, 'navigateTo')}>
                                    <View className='label'>
                                        <Image className='icon' src='https://file.open.banchengyun.com/2019/03/28/155375306526457.png' />
                                        <View className='name'>职位管理</View>
                                    </View>
                                    <View className='right-icon'></View>
                                </View>
                                <View className='row' onClick={this.toWhere.bind(this, '/subPackages/subPackageF/balanceInfo/balanceInfo', 'navigateTo')}>
                                    <View className='label'>
                                        <Image className='icon' src='https://file.open.banchengyun.com/2019/01/28/154866876066093.png' />
                                        <View className='name'>我的余额</View>
                                    </View>
                                    <View className='right-icon'></View>
                                </View>
                                <View className='row' onClick={this.toWhere.bind(this, '/subPackages/subPackageF/balancePhone/balancePhone', 'navigateTo')}>
                                    <View className='label'>
                                        <Image className='icon' src='https://file.open.banchengyun.com/2019/06/14/156048400426477.png' />
                                        <View className='name'>查看已购电话</View>
                                    </View>
                                    <View className='right-icon'></View>
                                </View>
                            </View>
                        </MoreFormid>
                        <Block>
                            <View className='cell'>
                                <Block>
                                    <View className='row' onClick={this.toWhere.bind(this, '/subPackages/subPackageF/shopApprove/shopApprove', 'navigateTo')}>
                                        <View className='label'>
                                            <Image className='icon' src='https://file.open.banchengyun.com/2019/03/31/155401583281272.png' />
                                            <View className='name'>企业认证</View>
                                        </View>
                                        <View className='right'>
                                            {myInfo.company_auth == null && <View className='text'>提交认证</View>}
                                            {myInfo.company_auth != null && myInfo.company_auth.auth_status == 0 && myInfo.company_auth.auth_image && <View className='text'>审核中</View>}
                                            {myInfo.company_auth != null && myInfo.company_auth.auth_status == 1 && myInfo.company_auth.auth_image && <View className='text'>审核通过</View>}
                                            {myInfo.company_auth != null && myInfo.company_auth.auth_status == 2 && myInfo.company_auth.auth_image && <View className='text'>认证失败，重新认证</View>}
                                            <View className='right-icon'></View>
                                        </View>
                                    </View>
                                </Block>
                            </View >
                        </Block >
                        <View className='protocol'>
                            我已阅读并同意<View className='proName' onClick={this.toWhere.bind(this, '/subPackages/subPackageS/protocol/protocol?type=company', 'navigateTo')}>《{company_protocol_title}》</View>
                        </View>
                        <View className='jumpBlock'>
                            <MoreFormid><View className='jump' onClick={this.toWhere.bind(this, '/pages/consumer/index/index', 'redirectTo')}>切换至用户端</View></MoreFormid>
                        </View>
                        <Footer></Footer>
                    </ScrollView>
                }
                <Tabbar currentPage={currentPage}></Tabbar>
                {changeLoad && <DataLoading></DataLoading>}
            </View >
        )
    }
}

export default My
